package com.esri.core.tasks.geodatabase;

import com.esri.core.internal.io.handler.h;
import com.esri.core.internal.io.handler.n;
import com.esri.core.internal.util.d;
import com.esri.core.io.UserCredentials;
import com.esri.core.map.ServerError;
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;

/* loaded from: classes.dex */
public class GeodatabaseStatusInfo {
    private static final String ERROR_FIELD = "error";
    private static final String LAST_UPDATED_TIME_FIELD = "lastUpdatedTime";
    private static final String LAYER_SERVER_GENS_FIELD = "layerServerGens";
    private static final String REPLICA_ID_FIELD = "replicaID";
    private static final String REPLICA_NAME_FIELD = "replicaName";
    private static final String REPLICA_SERVER_GEN_FIELD = "replicaServerGen";
    private static final String RESPONSE_TYPE_FIELD = "responseType";
    private static final String RESULT_URL_FIELD = "resultUrl";
    private static final String STATUS_FIELD = "status";
    private static final String SUBMISSION_TIME_FIELD = "submissionTime";
    private static final String UPDATE_URL_FIELD = "updateUrl";
    private long _downloadSize;
    private ServerError _error;
    private boolean _isDownloading;
    private String _jobId;
    private long _lastUpdateTime;
    private HashMap<Integer, Long> _layerServerGens;
    private String _replicaId;
    private String _replicaName;
    private long _replicaServerGen;
    private ResponseType _responseType;
    private String _resultUrl;
    private Status _status;
    private long _submissionTime;
    private long _totalRead;
    private String _updateUrl;

    /* loaded from: classes.dex */
    public enum Status {
        PENDING("Pending"),
        IN_PROGRESS("InProgress"),
        COMPLETED("Completed"),
        FAILED("Failed"),
        IMPORT_CHANGES("ImportChanges"),
        EXPORT_CHANGES("ExportChanges"),
        EXPORTING_DATA("ExportingData"),
        EXPORTING_SNAPSHOT("ExportingSnapshot"),
        EXPORT_ATTACHMENTS("ExportAttachments"),
        IMPORT_ATTACHMENTS("ImportAttachments"),
        PROVISIONING("ProvisioningReplica"),
        UNREGISTERING("UnRegisteringReplica"),
        COMPLETED_WITH_ERRORS("CompletedWithErrors"),
        EXCEPTION("Exception");
        
        private String _value;

        Status(String str) {
            this._value = str;
        }

        public String getValue() {
            return this._value;
        }

        public static Status fromString(String str) {
            if ("Pending".equals(str)) {
                return PENDING;
            }
            if ("InProgress".equals(str)) {
                return IN_PROGRESS;
            }
            if ("Completed".equals(str)) {
                return COMPLETED;
            }
            if ("Failed".equals(str)) {
                return FAILED;
            }
            if ("ImportChanges".equals(str)) {
                return IMPORT_CHANGES;
            }
            if ("ExportChanges".equals(str)) {
                return EXPORT_CHANGES;
            }
            if ("ExportingData".equals(str)) {
                return EXPORTING_DATA;
            }
            if ("ExportingSnapshot".equals(str)) {
                return EXPORTING_SNAPSHOT;
            }
            if ("ExportAttachments".equals(str)) {
                return EXPORT_ATTACHMENTS;
            }
            if ("ImportAttachments".equals(str)) {
                return IMPORT_ATTACHMENTS;
            }
            if ("ProvisioningReplica".equals(str)) {
                return PROVISIONING;
            }
            if ("UnRegisteringReplica".equals(str)) {
                return UNREGISTERING;
            }
            if ("CompletedWithErrors".equals(str)) {
                return COMPLETED_WITH_ERRORS;
            }
            if ("Exception".equals(str)) {
                return EXCEPTION;
            }
            PrintStream printStream = System.out;
            printStream.println("Unknown status: " + str);
            return null;
        }

        @Override // java.lang.Enum
        public String toString() {
            return this._value;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public enum ResponseType {
        NO_EDITS("esriReplicaResponseTypeNoEdits"),
        REGISTER_DATA("esriReplicaResponseTypeRegisterData"),
        INFO("esriReplicaResponseTypeInfo"),
        NORMAL("");
        
        private String _value;

        ResponseType(String str) {
            this._value = str;
        }

        public String getValue() {
            return this._value;
        }

        public static ResponseType fromString(String str) {
            if ("esriReplicaResponseTypeNoEdits".equals(str)) {
                return NO_EDITS;
            }
            if ("esriReplicaResponseTypeInfo".equals(str)) {
                return INFO;
            }
            return NORMAL;
        }

        @Override // java.lang.Enum
        public String toString() {
            return this._value;
        }
    }

    public static GeodatabaseStatusInfo fromJson(JsonParser jsonParser) throws Exception {
        if (d.c(jsonParser)) {
            GeodatabaseStatusInfo geodatabaseStatusInfo = new GeodatabaseStatusInfo();
            fromJsonInternal(jsonParser, geodatabaseStatusInfo);
            return geodatabaseStatusInfo;
        }
        return null;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public GeodatabaseStatusInfo(String str, String str2) {
        this._status = Status.PENDING;
        this._responseType = ResponseType.NORMAL;
        this._replicaServerGen = -1L;
        this._layerServerGens = new HashMap<>();
        String str3 = str + "/jobs/" + str2;
        this._updateUrl = str3;
        this._jobId = str2;
        if (str3 == null) {
            this._error = new ServerError("Part of the request or sync. process has failed and no status url is available.");
            this._status = Status.FAILED;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public GeodatabaseStatusInfo(String str) {
        this._status = Status.PENDING;
        this._responseType = ResponseType.NORMAL;
        this._replicaServerGen = -1L;
        this._layerServerGens = new HashMap<>();
        this._updateUrl = str;
        if (str == null) {
            this._error = new ServerError("Part of the request or sync. process has failed and no status url is available.");
            this._status = Status.FAILED;
        }
    }

    private GeodatabaseStatusInfo() {
        this._status = Status.PENDING;
        this._responseType = ResponseType.NORMAL;
        this._replicaServerGen = -1L;
        this._layerServerGens = new HashMap<>();
    }

    public GeodatabaseStatusInfo(Exception exc) {
        this._status = Status.PENDING;
        this._responseType = ResponseType.NORMAL;
        this._replicaServerGen = -1L;
        this._layerServerGens = new HashMap<>();
        this._error = new ServerError(exc);
        this._status = Status.EXCEPTION;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public GeodatabaseStatusInfo(long j, long j2) {
        this._status = Status.PENDING;
        this._responseType = ResponseType.NORMAL;
        this._replicaServerGen = -1L;
        this._layerServerGens = new HashMap<>();
        this._isDownloading = true;
        this._totalRead = j;
        this._downloadSize = j2;
    }

    public String getGeodatabaseName() {
        return this._replicaName;
    }

    public String getGeodatabaseId() {
        return this._replicaId;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public long getReplicaServerGen() {
        return this._replicaServerGen;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public Map<Integer, Long> getLayerServerGens() {
        return this._layerServerGens;
    }

    public long getSubmissionTime() {
        return this._submissionTime;
    }

    public long getLastUpdatedTime() {
        return this._lastUpdateTime;
    }

    public Status getStatus() {
        return this._status;
    }

    public String getResultUrl() {
        return this._resultUrl;
    }

    public String getUpdateUrl() {
        return this._updateUrl;
    }

    public void setUpdateUrl(String str) {
        this._updateUrl = str;
    }

    public String getJobId() {
        return this._jobId;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public ResponseType getResponseType() {
        return this._responseType;
    }

    public boolean hasNoEdits() {
        return this._responseType == ResponseType.NO_EDITS;
    }

    public ServerError getError() {
        return this._error;
    }

    public boolean isDownloading() {
        return this._isDownloading;
    }

    public long getTotalBytesDownloaded() {
        return this._totalRead;
    }

    public long getDownloadSize() {
        return this._downloadSize;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public boolean succeeded() {
        return Status.COMPLETED == this._status;
    }

    public void updateFromResult(UserCredentials userCredentials) throws Exception {
        if (this._responseType == ResponseType.REGISTER_DATA) {
            String str = this._resultUrl;
            JsonParser a = h.a(str, (Map<String, String>) null, n.a(str, userCredentials));
            if (a != null && d.c(a)) {
                fromJsonInternal(a, this);
            }
            if (a != null) {
                a.close();
            }
        }
    }

    public String toJson() throws IOException {
        StringWriter stringWriter = new StringWriter();
        JsonGenerator a = d.a(stringWriter);
        a.writeStartObject();
        a.writeStringField(RESPONSE_TYPE_FIELD, this._responseType.getValue());
        long j = this._replicaServerGen;
        if (j > -1) {
            a.writeNumberField(REPLICA_SERVER_GEN_FIELD, j);
        }
        if (!this._layerServerGens.isEmpty()) {
            a.writeFieldName(LAYER_SERVER_GENS_FIELD);
            a.writeStartArray();
            for (Integer num : this._layerServerGens.keySet()) {
                a.writeStartObject();
                a.writeNumberField("id", num.intValue());
                a.writeNumberField("serverGen", this._layerServerGens.get(num).longValue());
                a.writeEndObject();
            }
            a.writeEndArray();
        }
        a.writeStringField(RESULT_URL_FIELD, this._resultUrl);
        a.writeStringField(REPLICA_NAME_FIELD, this._replicaName);
        a.writeStringField(REPLICA_ID_FIELD, this._replicaId);
        a.writeNumberField(SUBMISSION_TIME_FIELD, this._submissionTime);
        a.writeNumberField(LAST_UPDATED_TIME_FIELD, this._lastUpdateTime);
        a.writeStringField("status", this._status.getValue());
        a.writeStringField(UPDATE_URL_FIELD, this._updateUrl);
        a.writeEndObject();
        a.close();
        return stringWriter.toString();
    }

    private static void fromJsonInternal(JsonParser jsonParser, GeodatabaseStatusInfo geodatabaseStatusInfo) throws Exception {
        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
            String currentName = jsonParser.getCurrentName();
            jsonParser.nextToken();
            if (currentName.equals(REPLICA_NAME_FIELD)) {
                geodatabaseStatusInfo._replicaName = jsonParser.getText();
            } else if (currentName.equals(REPLICA_SERVER_GEN_FIELD)) {
                geodatabaseStatusInfo._replicaServerGen = jsonParser.getLongValue();
            } else if (currentName.equals(LAYER_SERVER_GENS_FIELD)) {
                if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
                    parseLayerInfo(jsonParser, geodatabaseStatusInfo);
                } else if (jsonParser.getCurrentToken() == JsonToken.VALUE_STRING) {
                    JsonParser c = d.c(jsonParser.getText());
                    c.nextToken();
                    if (c.getCurrentToken() == JsonToken.START_ARRAY) {
                        parseLayerInfo(c, geodatabaseStatusInfo);
                    }
                    c.close();
                }
            } else if (currentName.equals(SUBMISSION_TIME_FIELD)) {
                geodatabaseStatusInfo._submissionTime = jsonParser.getLongValue();
            } else if (currentName.equals(LAST_UPDATED_TIME_FIELD)) {
                geodatabaseStatusInfo._lastUpdateTime = jsonParser.getLongValue();
            } else if (currentName.equals("status")) {
                geodatabaseStatusInfo._status = Status.fromString(jsonParser.getText());
            } else if (currentName.equals(RESULT_URL_FIELD)) {
                geodatabaseStatusInfo._resultUrl = jsonParser.getText();
            } else if (currentName.equals(UPDATE_URL_FIELD)) {
                geodatabaseStatusInfo._updateUrl = jsonParser.getText();
            } else if (currentName.equals(RESPONSE_TYPE_FIELD)) {
                geodatabaseStatusInfo._responseType = ResponseType.fromString(jsonParser.getText());
            } else if (currentName.equals(ERROR_FIELD)) {
                geodatabaseStatusInfo._error = ServerError.fromJson(jsonParser);
                geodatabaseStatusInfo._status = Status.FAILED;
            } else if (currentName.equals(REPLICA_ID_FIELD)) {
                geodatabaseStatusInfo._replicaId = jsonParser.getText();
            } else {
                jsonParser.skipChildren();
            }
        }
    }

    private static void parseLayerInfo(JsonParser jsonParser, GeodatabaseStatusInfo geodatabaseStatusInfo) throws IOException, JsonParseException {
        while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
            if (jsonParser.getCurrentToken() == JsonToken.START_OBJECT) {
                int i = -1;
                long j = -1;
                while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                    String currentName = jsonParser.getCurrentName();
                    jsonParser.nextToken();
                    if ("id".equals(currentName)) {
                        i = jsonParser.getIntValue();
                    } else if ("serverGen".equals(currentName)) {
                        j = jsonParser.getLongValue();
                    } else {
                        jsonParser.skipChildren();
                    }
                }
                geodatabaseStatusInfo.getLayerServerGens().put(new Integer(i), new Long(j));
            }
        }
    }
}
